// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "item_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "收藏项目ID",
    "label": "收藏项目ID"
  },
  "item_type": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 1,
            "text": "商品"
          },
          {
            "value": 2,
            "text": "文章"
          },
          {
            "value": 3,
            "text": "商户"
          },
          {
            "value": 4,
            "text": "非遗传人"
          }
        ]
      }
    ],
    "title": "收藏类型",
    "label": "收藏类型"
  },
  "item_title": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 200
      }
    ],
    "title": "收藏项目标题",
    "label": "收藏项目标题"
  },
  "item_image": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收藏项目图片",
    "label": "收藏项目图片"
  },
  "item_description": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "收藏项目描述",
    "label": "收藏项目描述"
  },
  "item_selltype": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "商品售卖类型",
    "defaultValue": 0,
    "label": "商品售卖类型"
  },
  "item_price": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "商品价格",
    "label": "商品价格"
  },
  "item_author": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "作者/商户",
    "label": "作者/商户"
  },
  "item_rating": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0,
        "maximum": 5
      }
    ],
    "title": "评分",
    "label": "评分"
  },
  "create_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "收藏时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "收藏时间"
  },
  "user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "收藏者ID",
    "label": "收藏者ID"
  }
}

const enumConverter = {
  "item_type_valuetotext": {
    "1": "商品",
    "2": "文章",
    "3": "商户",
    "4": "非遗传人"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
